package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.AdminBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.AdminDao;
import com.jackiechancinema.ssm.dto.AdminDto;
import com.jackiechancinema.ssm.dto.AdminExcelDto;
import com.jackiechancinema.ssm.dto.SerchDto;
import com.jackiechancinema.ssm.entity.Admin;
import com.jackiechancinema.ssm.listener.AdminReadListener;
import com.jackiechancinema.ssm.service.Inte.AdminService;
import com.jackiechancinema.ssm.utils.EncryptUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminDao adminDao;
    @Autowired
    private AdminReadListener listener;
    @Override

    public PageVo<Admin> queryPage(AdminBo bo) {
        if(bo!=null){
            //1.组装查询条件
            Admin admin=new Admin();
            if(StringUtils.hasLength(bo.getName())){
                admin.setAdmin_name(bo.getName());
            }
            if((bo.getFlag()!=null)&&(bo.getFlag()>-1)){
                admin.setAdmin_flag(bo.getFlag());
            }
            if((bo.getType()!=null)&&(bo.getType())>-1){
                admin.setAdmin_type(bo.getType());
            }
            //2.开启分页插件
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            //3.获取分页的查询结果
            PageInfo<Admin> pageInfo=new PageInfo<Admin>(adminDao.selectBySerch(admin));
            //4.返回结果
            return new PageVo<Admin>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Admin>(SystemContant.PAGE_R,"OK",0,null);
    }

    @Override
    public R saveAdmin(AdminDto adminDto) {
        Admin admin=new Admin();
        admin.setAdmin_name(adminDto.getName());
        admin.setAdmin_type(adminDto.getType());
        admin.setAdmin_password(EncryptUtil.aesenc(SystemContant.PASS_KEY,adminDto.getPassword()));
        if(adminDao.insert(admin)>0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Admin admin) {

        if(admin.getAdmin_password()!=null){
            admin.setAdmin_password(EncryptUtil.aesenc(SystemContant.PASS_KEY,admin.getAdmin_password()));
            if(adminDao.update(admin)>0){
                return R.ok(admin);
            }else {
                return R.fail();
            }
        }else {
            if(adminDao.update(admin)>0){
                return R.ok(admin);
            }else {
                return R.fail();
            }
        }

    }

    @Override
    public R delete(int id) {
        if(adminDao.deleteById(id)>0){
            return R.ok("OK");
        }
        return R.fail();
    }

    @Override
    public R updatePassWord(String oldpass, String newpass, HttpSession session) {
        Admin admin = (Admin)session.getAttribute("curruser");
        if(admin != null){
            if(admin.getAdmin_password().equals(EncryptUtil.aesenc(SystemContant.PASS_KEY,oldpass))){
                int result = adminDao.updatePassword(admin.getAdmin_id(),EncryptUtil.aesenc(SystemContant.PASS_KEY,newpass));
                if(result>0){
                    return R.ok("OK");
                }
            }
        }
        return R.fail();
    }

    @Override
    public R batchDel(List<Admin> adminList) {
        List<Integer> list = new ArrayList<>();
        for (Admin admin : adminList) {
            list.add(admin.getAdmin_id());
        }
        if(adminDao.deleteBatch(list)>0){
            return R.ok("OK");
        }else{
            return R.fail();
        }
    }

    @Override
    public R serch(SerchDto serchDto) {
        List<Admin> adminList = adminDao.search(serchDto.getName(),serchDto.getType(),serchDto.getFlag());
        if(adminList!=null){
            R r = R.ok(adminList);
            System.out.println(r);
            return r;
        }else{
            return R.fail();
        }
    }

    @Override
    public R batchAdd(MultipartFile file) {
        if(!file.isEmpty()){
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), AdminExcelDto.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(AdminBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Admin admin=new Admin();

        if(StringUtils.hasLength(bo.getName())){
            admin.setAdmin_name(bo.getName());
        }
        if((bo.getFlag()!=null)&&(bo.getFlag()>-1)){
            admin.setAdmin_flag(bo.getFlag());
        }
        if((bo.getType()!=null)&&(bo.getType())>-1){
            admin.setAdmin_type(bo.getType());
        }
        //3.获取要下载的数据
        List<Admin> list=adminDao.selectBySerch(admin);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Admin.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
